package com.example.picturechanging

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.picturechanging.ui.theme.PictureChangingTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            PictureChangingTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    PictureGallery()
                }
            }
        }
    }
}

// 数据类，用于存储图片和描述信息
data class GalleryItem(
    val imageRes: Int,
    val title: String,
    val artist: String,
    val year: Int
)

@Composable
fun PictureGallery() {
    // 创建图片画廊数据
    val galleryItems = remember {
        listOf(
            GalleryItem(
                imageRes = R.drawable.image1, // 请替换为你的图片资源
                title = "Still Life of Blue Rose and Other Flowers",
                artist = "Owen Scott",
                year = 2021
            ),
            GalleryItem(
                imageRes = R.drawable.image2, // 请替换为你的图片资源
                title = "Mountain Landscape",
                artist = "Sarah Johnson",
                year = 2020
            ),
            GalleryItem(
                imageRes = R.drawable.image3, // 请替换为你的图片资源
                title = "Urban Cityscape",
                artist = "Michael Chen",
                year = 2022
            )
            // 可以添加更多图片...
        )
    }

    // 使用 remember 保存当前显示的图片索引
    var currentIndex by remember { mutableStateOf(0) }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {
        // 显示当前时间（模拟你的截图）
        Text(
            text = "12:00",
            fontSize = 32.sp,
            fontWeight = FontWeight.Bold,
            color = MaterialTheme.colorScheme.primary,
            modifier = Modifier.padding(bottom = 32.dp)
        )

        // 图片显示区域
        Card(
            modifier = Modifier
                .fillMaxWidth()
                .height(300.dp),
            elevation = CardDefaults.cardElevation(defaultElevation = 8.dp)
        ) {
            Image(
                painter = painterResource(id = galleryItems[currentIndex].imageRes),
                contentDescription = galleryItems[currentIndex].title,
                modifier = Modifier.fillMaxSize(),
                contentScale = ContentScale.Crop
            )
        }

        Spacer(modifier = Modifier.height(24.dp))

        // 图片描述区域
        Column(
            modifier = Modifier.fillMaxWidth(),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(
                text = galleryItems[currentIndex].title,
                fontSize = 18.sp,
                fontWeight = FontWeight.SemiBold,
                textAlign = TextAlign.Center,
                modifier = Modifier.padding(bottom = 8.dp)
            )

            Text(
                text = "${galleryItems[currentIndex].artist} (${galleryItems[currentIndex].year})",
                fontSize = 14.sp,
                color = MaterialTheme.colorScheme.onSurfaceVariant,
                textAlign = TextAlign.Center
            )
        }

        Spacer(modifier = Modifier.height(32.dp))

        // 导航按钮区域
        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            // 上一个按钮
            Button(
                onClick = {
                    // 循环到上一张图片
                    currentIndex = if (currentIndex - 1 >= 0) {
                        currentIndex - 1
                    } else {
                        galleryItems.size - 1
                    }
                },
                modifier = Modifier.width(120.dp),
                enabled = galleryItems.size > 1
            ) {
                Text("Previous")
            }

            // 下一个按钮
            Button(
                onClick = {
                    // 循环到下一张图片
                    currentIndex = (currentIndex + 1) % galleryItems.size
                },
                modifier = Modifier.width(120.dp),
                enabled = galleryItems.size > 1
            ) {
                Text("Next")
            }
        }

        // 指示器（显示当前图片位置）
        if (galleryItems.size > 1) {
            Text(
                text = "${currentIndex + 1} / ${galleryItems.size}",
                fontSize = 14.sp,
                color = MaterialTheme.colorScheme.onSurfaceVariant,
                modifier = Modifier.padding(top = 16.dp)
            )
        }
    }
}

@Preview(showBackground = true)
@Composable
fun PictureGalleryPreview() {
    PictureChangingTheme {
        PictureGallery()
    }
}